package com.example.hcd.tool;

import com.example.hcd.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

/**
 * 自定义弹出框 daiye 2015/07/16
 */
public class CommonDialog extends Dialog implements View.OnClickListener
{

	private Context context;
	private LinearLayout mLinearLayout;
	private LinearLayout mViewGroup;

	public CommonDialog(Context context) {
		super(context, R.style.select_dialog);
		this.context = context;
		mViewGroup =new LinearLayout(context);
		mViewGroup.setOrientation(LinearLayout.VERTICAL);
//		mViewGroup.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics())));
		mViewGroup.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
	}

	public void addView(View view)
	{
//		TextView v = new TextView(context);
//		((TextView) v).setTextAppearance(context, R.style.line_horizontal_normal);
//		mLinearLayout.addView(v);
		
		mViewGroup.addView(view);
//		view.setText(text);
//		view.findViewById(id).setOnClickListener(this);
	}

	@Override
	protected final void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.common_dialog);
		// initTextItemStatus();
		findViewById(R.id.cancel_btn).setOnClickListener(this);
		mLinearLayout = (LinearLayout) findViewById(R.id.addline);
		mLinearLayout.addView(mViewGroup);
		setCanceledOnTouchOutside(true);
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {

		case R.id.cancel_btn:
			this.dismiss();
			break;
		}
		new android.os.Handler().postDelayed(new Runnable() {
			@Override
			public void run()
			{
				if (null != CommonDialog.this && CommonDialog.this.isShowing())
				{
					CommonDialog.this.dismiss();
				}
			}
		}, 200);
	}

	@Override
	public void setTitle(CharSequence title)
	{

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		return super.onKeyUp(keyCode, event);
	}

	@Override
	public void show()
	{
		if (context != null && ((Activity) context).isFinishing())
		{
			return;
		}
		super.show();
	}

}
